package com.chenyue.cm.push.jpush;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.alibaba.fastjson.JSON;
import com.chenyue.cm.push.jpush.config.PushConfig;
import com.chenyue.cm.push.jpush.vo.PushContent;
import com.chenyue.cm.push.jpush.vo.PushParam;

/**
 * @author libingxing
 */
public class PushUtils {

    /**
     * 通知推送 single
     *
     * @param param 通知参数vo
     * @return 推送实体
     */
    public static PushPayload buildPushNotifyAliasFollowUser(PushParam param) {
        return PushPayload.newBuilder()
//                平台
                .setPlatform(Platform.android_ios())
//                接收人群
                .setAudience(param.getNotifyTo() == null ? Audience.all() : Audience.alias(param.getNotifyTo() + ""))
//                发送通知
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .addExtra("param", JSON.toJSONString(param.getPushContent()))
                                .setAlert(param.getPushContent().getContent())
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .addExtra("param", JSON.toJSONString(param.getPushContent()))
                                .setAlert(param.getPushContent().getContent())
                                .build())
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)
                        .setTimeToLive(PushConfig.TIME_TO_LIVE)
                        .build())
                .build();
    }

    public static PushPayload myPushPayLoad(PushParam param) {
        return PushPayload.newBuilder()
//                平台
                .setPlatform(Platform.android_ios())
//                接收人群
                .setAudience(Audience.alias(param.getNotifyTo() + ""))
//                发送通知
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .addExtra("param", JSON.toJSONString(param.getPushContent()))
                                .setAlert(param.getPushContent().getContent())
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .addExtra("param", JSON.toJSONString(param.getPushContent()))
                                .setAlert(param.getPushContent().getContent())
                                .build())
                        .build())
                .setOptions(Options.newBuilder()
//                        是否正式推送 IOS配置 开发false 上线true
                        .setApnsProduction(false)
                        .setTimeToLive(PushConfig.TIME_TO_LIVE)
                        .build())
                .build();
    }

    public static void myPush(PushParam param) {
        try {
            JPushClient client = new JPushClient(PushConfig.MASTER_SECRET, PushConfig.APP_KEY, null, ClientConfig.getInstance());
            //生成推送的内容
            PushPayload payload = myPushPayLoad(param);
            payload.resetOptionsTimeToLive(PushConfig.TIME_TO_LIVE);
            PushResult result = client.sendPush(payload);
            PushConfig.LOGGER.info("Got result - " + result);
        } catch (APIConnectionException e) {
            PushConfig.LOGGER.error("Connection error, should retry later", e);
        } catch (APIRequestException e) {
            PushConfig.LOGGER.error("Should review the error, and fix the request", e);
            PushConfig.LOGGER.info("HTTP Status: " + e.getStatus());
            PushConfig.LOGGER.info("Error Code: " + e.getErrorCode());
            PushConfig.LOGGER.info("Error Message: " + e.getErrorMessage());
        }
    }

    public static void push(PushParam param) {
        try {
            JPushClient client = new JPushClient(PushConfig.MASTER_SECRET, PushConfig.APP_KEY, null, ClientConfig.getInstance());
            //生成推送的内容
            PushPayload payload = buildPushNotifyAliasFollowUser(param);
            payload.resetOptionsTimeToLive(PushConfig.TIME_TO_LIVE);
            PushResult result = client.sendPush(payload);
            PushConfig.LOGGER.info("Got result - " + result);
        } catch (APIConnectionException e) {
            PushConfig.LOGGER.error("Connection error, should retry later", e);
        } catch (APIRequestException e) {
            PushConfig.LOGGER.error("Should review the error, and fix the request", e);
            PushConfig.LOGGER.info("HTTP Status: " + e.getStatus());
            PushConfig.LOGGER.info("Error Code: " + e.getErrorCode());
            PushConfig.LOGGER.info("Error Message: " + e.getErrorMessage());
        }

    }

    public static void main(String[] args) {
        PushContent content = new PushContent();
        content.setParam("");
        content.setContent("测试一下推送");
        PushParam param = new PushParam(145, content, 2);
        PushUtils.push(param);
    }


}
